package com.wnxy.web.servlet;

import com.wnxy.bean.User;
import com.wnxy.service.impl.UserServiceImpl;
import com.wnxy.util.BeanUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Map;

@WebServlet("/user")
public class UserServlet  extends BaseServlet{

    public  String addUser(HttpServletRequest req, HttpServletResponse res) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException, IOException {

         req.setCharacterEncoding("UTF-8");

         res.setContentType("text/html;charset=utf-8");


        User user= BeanUtil.populate(User.class,req.getParameterMap());


        Map<String,String[]> map=req.getParameterMap();

        for(Map.Entry<String,String[]> entry: map.entrySet()){
            System.out.println(entry.getKey());

            System.out.println(Arrays.toString(entry.getValue()));
        }


        UserServiceImpl  ss=new UserServiceImpl();

        boolean flag=ss.addUser(user);

        res.getWriter().write(String.valueOf(flag));

        return null;

    }



   public String loginUser(HttpServletRequest req,HttpServletResponse resp) throws IOException {

       req.setCharacterEncoding("UTF-8");

       resp.setContentType("text/html;charset=utf-8");


       String name=req.getParameter("userName");


       String pwd=req.getParameter("userPass");


       UserServiceImpl  ui=new UserServiceImpl();

       User user=ui.loginUser(name,pwd);


       if(user!=null){

           HttpSession session=req.getSession();

           session.setAttribute("loginUser",user);

           resp.getWriter().write("true");

       }
       else{

           resp.getWriter().write("false");
       }

       return null;

   }

}
